package cn.sumpu.app.coolsk.dao.impl;

import java.util.*;

import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.slf4j.*;
import cn.sumpu.app.coolsk.dao.*;
import cn.sumpu.app.coolsk.domain.*;
import cn.sumpu.app.coolsk.exception.AppBusinessException;

public class ContentDaoImpl extends SqlSessionDaoSupport implements ContentDao {
	private final static Logger logger = LoggerFactory
			.getLogger(ContentDaoImpl.class);

	//取得后台内容
	@SuppressWarnings("unchecked")
	@Override
	public List<Content> getBackContent(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return getSqlSession().selectList(
					"cn.sumpu.app.coolsk.persistence.ContentMapper.getBackContent", map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getBackContent resource failure.", e);
			}
			throw new AppBusinessException("getBackContent resource failure.", e);
		}
	}
	//添加内容
	@Override
	public void addContent(Content content) throws AppBusinessException {
		try {
			getSqlSession().selectList(
					"cn.sumpu.app.coolsk.persistence.ContentMapper.addContent", content);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("addContent resource failure.", e);
			}
			throw new AppBusinessException("addContent resource failure.", e);
		}
	}
	//删除内容
	@Override
	public void deleteContent(String id) throws AppBusinessException {
		try {
			getSqlSession().delete(
					"cn.sumpu.app.coolsk.persistence.ContentMapper.deleteContent", id);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("deleteContent resource failure.", e);
			}
			throw new AppBusinessException("deleteContent resource failure.", e);
		}
	}
	//更新内容
	@Override
	public void updateContent(Content content) throws AppBusinessException {
		try {
			getSqlSession().update(
					"cn.sumpu.app.coolsk.persistence.ContentMapper.updateContent", content);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("updateContent resource failure.", e);
			}
			throw new AppBusinessException("updateContent resource failure.", e);
		}
	}
	//取得后台内容总条数
	@Override
	public Integer getBackContentCount(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return (Integer) getSqlSession().selectOne(
					"cn.sumpu.app.coolsk.persistence.ContentMapper.getBackContentCount", map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getBackContentCount resource failure.", e);
			}
			throw new AppBusinessException("getBackContentCount resource failure.", e);
		}
	}
	//根据条件查询存在多少条
	@Override
	public Integer getContentExists(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return (Integer) getSqlSession().selectOne(
					"cn.sumpu.app.coolsk.persistence.ContentMapper.getContentExists", map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getContentExists resource failure.", e);
			}
			throw new AppBusinessException("getContentExists resource failure.", e);
		}
	}
	//通过ID取得某条内容
	@Override
	public Content getContentById(String id) throws AppBusinessException {
		try {
			return (Content) getSqlSession().selectOne(
					"cn.sumpu.app.coolsk.persistence.ContentMapper.getContentById", id);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getContentById resource failure.", e);
			}
			throw new AppBusinessException("getContentById resource failure.", e);
		}
	}
	//根据栏目取得内容(在前台展示)
	@SuppressWarnings("unchecked")
	@Override
	public List<Content> getContentByChannel(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return getSqlSession().selectList(
					"cn.sumpu.app.coolsk.persistence.ContentMapper.getContentByChannel", map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getContentByChannel resource failure.", e);
			}
			throw new AppBusinessException("getContentByChannel resource failure.", e);
		}
	}
	//查询推送的门票
	@SuppressWarnings("unchecked")
	@Override
	public List<Content> getTicketByChannel(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return getSqlSession().selectList(
					"cn.sumpu.app.coolsk.persistence.ContentMapper.getTicketByChannel", map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getTicketByChannel resource failure.", e);
			}
			throw new AppBusinessException("getTicketByChannel resource failure.", e);
		}
	}
	//查询推送的酒店
	@SuppressWarnings("unchecked")
	@Override
	public List<Content> getHotelByChannel(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return getSqlSession().selectList(
					"cn.sumpu.app.coolsk.persistence.ContentMapper.getHotelByChannel", map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getHotelByChannel resource failure.", e);
			}
			throw new AppBusinessException("getHotelByChannel resource failure.", e);
		}
	}
	//查询推送的产品
	@SuppressWarnings("unchecked")
	@Override
	public List<Content> getProductByChannel(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return getSqlSession().selectList(
					"cn.sumpu.app.coolsk.persistence.ContentMapper.getProductByChannel", map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getProductByChannel resource failure.", e);
			}
			throw new AppBusinessException("getProductByChannel resource failure.", e);
		}
	}
	//查询推送的景区
	@SuppressWarnings("unchecked")
	@Override
	public List<Content> getScenicByChannel(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return getSqlSession().selectList(
					"cn.sumpu.app.coolsk.persistence.ContentMapper.getScenicByChannel", map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getScenicByChannel resource failure.", e);
			}
			throw new AppBusinessException("getScenicByChannel resource failure.", e);
		}
	}
	//查询推送的点评
	@SuppressWarnings("unchecked")
	@Override
	public List<Content> getDiscussByChannel(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return getSqlSession().selectList(
					"cn.sumpu.app.coolsk.persistence.ContentMapper.getDiscussByChannel", map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getDiscussByChannel resource failure.", e);
			}
			throw new AppBusinessException("getDiscussByChannel resource failure.", e);
		}
	}
	@Override
	public Integer getContentByChannelCount(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return (Integer) getSqlSession().selectOne(
					"cn.sumpu.app.coolsk.persistence.ContentMapper.getContentByChannelCount", map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getContentByChannelCount resource failure.", e);
			}
			throw new AppBusinessException("getContentByChannelCount resource failure.", e);
		}
	}
	//查询推送的行程
	@SuppressWarnings("unchecked")
	@Override
	public List<Content> getTripByChannel(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return getSqlSession().selectList(
					"cn.sumpu.app.coolsk.persistence.ContentMapper.getTripByChannel", map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getTripByChannel resource failure.", e);
			}
			throw new AppBusinessException("getTripByChannel resource failure.", e);
		}
	}
	//通过频道查询最小的排序号
	@Override
	public Integer getMinSortByChannel(String channel)
			throws AppBusinessException {
		try {
			return (Integer) getSqlSession().selectOne(
					"cn.sumpu.app.coolsk.persistence.ContentMapper.getMinSortByChannel", channel);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getMinSortByChannel resource failure.", e);
			}
			throw new AppBusinessException("getMinSortByChannel resource failure.", e);
		}
	}
	//根据条件查询排序内容
	@Override
	public Content getSortByCondition(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return (Content) getSqlSession().selectOne(
					"cn.sumpu.app.coolsk.persistence.ContentMapper.getSortByCondition", map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getSortByCondition resource failure.", e);
			}
			throw new AppBusinessException("getSortByCondition resource failure.", e);
		}
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Content> getContent(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return getSqlSession().selectList(
					"cn.sumpu.app.coolsk.persistence.ContentMapper.searchContent", map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("searchContent failure.", e);
			}
			throw new AppBusinessException("searchContent failure.", e);
		}
	}
	@Override
	public Integer getContentCount(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return (Integer) getSqlSession().selectOne(
					"cn.sumpu.app.coolsk.persistence.ContentMapper.searchContentCount",map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("searchContentCount  failure.", e);
			}
			throw new AppBusinessException("searchContentCount  failure.", e);
		}
	}
	
}
